define(["utils"], function (utils) {
    function pageLogic(config) {
        this.pageview = config.pageview;

        this.params = this.pageview.showPageParams;
    }

    pageLogic.prototype = {
        header_title_init:function(sender,params){
            if(this.params.isTeam){
                sender.config.text = "团队成员";
            }else{
                sender.config.text = "部门成员";
            }
        },
        Arr2Dict:function(arr){
            var re = {};

            for(var i=0,j=arr.length;i<j;i++){
                var key =arr[i].month||arr[i].quarter;
                re[key] = arr[i].num;
            }
            return re;
        },
        list_table_parsedata:function(sender,params){
            var result = params.data;
            if(result.code!==0){
                return false;
            }
            var data = result.data.resultList||[];
            var count = this.timetype==="按月统计"?12:4;
            for(var i=0,j=data.length;i<j;i++){
                var resultList = data[i].resultList;
                var dict = this.Arr2Dict(resultList);
                var sum = 0;
                for(ii=1;ii<=count;ii++){
                    data[i]["field"+ii] = dict[ii.toString()]||0;
                    sum+=data[i]["field"+ii];
                }
                data[i].sum = sum;
            }
            return {data:result.data.resultList,sumCount:result.data.totalRows};
        },
        list_table_didmount:function(sender,params){
            //
            var ajaxParams = {
                years:this.params.years,orderType:this.params.orderType,templateId:this.params.templateId};
            if(this.params.isTeam){
                ajaxParams.groupId = this.params.departid_or_teamid;
            }else{
                ajaxParams.deptId = this.params.departid_or_teamid;
            }
            this.timetype = this.params.orderType===0?"按月统计":"按季度统计";
            this.createHeader(sender,this.timetype);
            sender.setAjaxConfigParams(ajaxParams);
            sender.loadPageDataByIndex(1);
        },
        NumArr:["一","二","三","四","五","六","七","八","九","十","十一","十二"],
        createHeader: function (sender,type) {
            this.timetype = type;
            var columns=[
                {
                  headerText:"姓名",
                  cell:["row_depart_name"],
                  headerStyle:{
                    width:"160px",
                    textAlign:"left",
                    textIndent:"10px"
                  },
                  cellStyle:{

                  }
              }];
              this.pageview.config.components.row_depart_name = {
                  type: "text",
                  style: {
                      textAlign:"left",
                      textIndent:"10px"
                  },
                  text_bind: "userName"
              };
              var count = type ==="按月统计"?12:4;
              var label = type ==="按月统计"?"月":"季度";
            for (var i = 0; i < count; i++) {
                var rowWidth = 50;

                var com_key = "text_" + i;
                columns.push({
                    headerText: this.NumArr[i]+label,
                    cell: [com_key],
                    headerStyle: {
                        width: rowWidth
                    }

                });
                this.pageview.config.components[com_key] = {
                    type: "text",
                    style: {
                        textAlign:"center"
                    },
                    text_bind: "field"+(i+1)
                };
            }

            columns.push({
                  headerText:"汇总",
                  cell:["sumlabel"],
                  headerStyle:{
                    width:"80px"
                  },
                  cellStyle:{

                  }
             });
             this.pageview.config.components.sumlabel = {
                 type: "text",
                 style: {
                     textAlign:"center"
                 },
                 text_bind: "sum"
             };


            sender.createHeader(columns);
        },
        header_close_click:function(){
            this.pageview.close();
        }

    };
    return pageLogic;
});
